//
//  BodyScrollView.m
//  WordTutor
//
//  Created by lyh on 15/11/18.
//  Copyright © 2015年 lyh. All rights reserved.
//

#import "BodyScrollView.h"
#import "EVAHeadView.h"
#import "MiddleView.h"
#import "StatementView.h"
#import "WordView.h"

@interface BodyScrollView ()<UIScrollViewDelegate>

@property (nonatomic, strong) MiddleView *middleView;
@property (nonatomic, strong) WordView *wordView;
@end

@implementation BodyScrollView

- (WordView *)wordView
{
    if (!_wordView) {
        self.wordView = [[WordView alloc] initWithFrame:CGRectMake(0, 64+ 375 * 0.25, 375, 375 * 0.5)];
    }
    return _wordView;
}

#pragma mark - 截断响应链
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // 把自己的点转换按钮的坐标系上的点
    CGPoint buttonPoint = [self convertPoint:point toView:self.wordView];
    
    if ([self.wordView pointInside:buttonPoint withEvent:event]) return nil;
    
    return [super hitTest:point withEvent:event];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sheight=scrollView.contentOffset.y;
    if(sheight>=0&&sheight<=260){
        CGFloat alpha=0.7f*sheight/260.0f;
        self.backgroundColor=[UIColor colorWithWhite:0.0f alpha:alpha];
        _headerView.backgroundColor=[UIColor colorWithWhite:0.0f alpha:alpha];
    }else if (sheight>260){
        self.backgroundColor=[UIColor colorWithWhite:0.0f alpha:0.5f];
        _headerView.backgroundColor=[UIColor colorWithWhite:0.0f alpha:0.5f];
    }
}

- (void)singleTapGestureCaptured:(id)sender{
    CGRect rect=self.frame;
    if(self.contentOffset.y>0){
        rect.origin=CGPointMake(0, 0);
    }else{
        rect.origin=CGPointMake(0, [UIScreen mainScreen].bounds.size.height-208.0f);
    }
    [self scrollRectToVisible:rect animated:YES];
}

- (void)createViewsWithSuperView:(UIView *) view{
    
//    CGRect bounds = [UIScreen mainScreen].bounds;
    
    
    _headerView=[[EVAHeadView alloc] initWithFrame:CGRectMake(0, 0, view.width, 64)];
    //[_headerView.backBtn addTarget:self action:@selector(backToMain) forControlEvents:UIControlEventTouchUpInside];
    //    [_headerView.refreshBtn addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_headerView];
    
    CGRect rect=view.bounds;
    rect.origin.y=64.0f;
    rect.size.height-=64.0f;
    self.frame=rect;
    
    _middleView=[[MiddleView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, rect.size.height)];
    //    [_curWeatherView fillViewWith:nil];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
    [_middleView addGestureRecognizer:singleTap];
    [self addSubview:_middleView];
    
    _statementView=[[StatementView alloc] initWithFrame:CGRectMake(10, _middleView.frame.origin.y+_middleView.frame.size.height, 355, 208)];
    [self addSubview:_statementView];
    
    self.contentSize=CGSizeMake(0, _statementView.frame.origin.y+_statementView.frame.size.height+60.0f
                                       );//_forecastWeatherView.frame.origin.y+_forecastWeatherView.frame.size.height+60.0f
    self.backgroundColor=[UIColor clearColor];
    self.showsVerticalScrollIndicator=NO;
    self.delegate=self;
    self.clipsToBounds=YES;
}


@end
